// page/my/index.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    account: {
      "id": 0,
      "userId": 0,
      "balance": 0,
      "isLock": 0
    },
    hasLogin: false,
    token: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/page/auth/login/login"
      });
    }
  },

  getAccount: function (url) {
    var that = this;
    console.log("getAccount token =" + this.data.token);
    wx.request({
      url: url,
      method: 'GET',
      header: {
        "Content-Type": "json",
        'X-AUTH-TOKEN': this.data.token
      },
      success: function (res) {
        console.log(res.data);    //控制台打印通过API获取的object对象，可展开查看结构
        if(res.data.retCode==-1){
          try {
            wx.removeStorageSync('userInfo');
            wx.removeStorageSync('token');
          } catch (e) {
            // Do something when catch error
          }
          wx.navigateTo({
            url: "/page/auth/login/login"
          });
        }
        that.setData({
          account: res.data
        })
      }
    });
  },

  signinjump() {
    wx.navigateTo({
      url: "/page/my/pages/signin/signin?user_id=1"
    });
  },

  goOrder() {
    wx.navigateTo({
      url: "/page/order/pages/list/list"
    });
  },

  workcheck() {
    wx.navigateTo({
      url: "/page/my/pages/check/index"
    });
  },

  goTask() {
    wx.navigateTo({
      url: "/page/my/pages/task/list"
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //console.log("onshow token:"+wx.getStorageSync('token'))
    if (app.globalData.hasLogin) {
      let userInfo = wx.getStorageSync('userInfo');
      var token = wx.getStorageSync('token');
      this.setData({
        userInfo: userInfo,
        hasLogin: true,
        token: token
      });

      var accountUrl = app.globalData.apiBase + app.globalData.accountUrl;
      console.log(accountUrl);
      this.getAccount(accountUrl);

    } else {
      wx.removeStorageSync('userInfo');
      wx.removeStorageSync('token');
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})